package com.zhouchb.stream;

import java.io.*;

/**
 * @Author Zhouchb
 * @Create 2021-10-2021/10/31 :21:02
 * @Description
 */
public class FileReadWriterTest {
    //
    /*
    一、流的分类
    1、操作单位：字节流，字符流
    2、数据的流向：输入流，输出流
    3、流的角色：节点流，处理流
    二、流的体系结构
    抽象基类            节点流(文件流）                缓冲流（处理流的一种）
    InputStream         FileInputStream                 BufferedInputStream
    OutputStream        FileOutputStream                BufferedOutputStream
    Reader              FileReader                      BufferedReader
    Writer              FileWriter                      BufferedWriter
     */
    public static void main(String[] args) throws Exception {
        fileReader1();
//        System.out.println("\n===========FileReader改进=============");
//        fileReader2();
//        fileWriter();
//        fileReaderAndWriter();
    }
    //字符流 读取磁盘中的字符文件
    public static void fileReader1() {
        FileReader fileReader = null;
        try {
            File file = new File("IO/Hello.txt");
            System.out.println(file.getAbsoluteFile());
            fileReader = new FileReader(file);
            int read = fileReader.read();  //返回一个字符
            while (read!=-1){  //如果读到文字末尾返回-1
                System.out.print((char)read);
                read = fileReader.read();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //流关闭
            try {
                if (fileReader!=null)
                fileReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    //FileReader 改进
    public static void fileReader2(){
        FileReader fr = null;
        try {
            File file = new File("IO/Hello.txt");
            fr = new FileReader(file);
            char[] chars = new char[5];
            int len;
            while ((len = fr.read(chars))!=-1){  //写入到字符数组中
                String s = new String(chars,0,len);
                System.out.print(s);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fr!=null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void fileWriter(){
        FileWriter fw = null;
        try {
            File file = new File("IO/Hello.txt");
           fw = new FileWriter(file,true);  //true 文件不覆盖输入
           fw.write("\nI have a dream!\n");
           fw.write("you need to have a dream!");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fw!=null)
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    //filereader 和 filewriter 结合
    public static void fileReaderAndWriter(){
        FileReader fr = null;
        FileWriter fw = null;
        try {
            File file = new File("IO/Hello.txt");
            File fileW = new File("IO/Hello2.txt");
            fr = new FileReader(file);
            fw = new FileWriter(fileW,true);
            char[] chars = new char[5];
            int len;
            while ((len=fr.read(chars))!=-1){
                String s = new String(chars, 0, len);
                fw.write(s);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fr!=null)
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fw!=null)
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
